package gs.wicket.app.login;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import gs.wicket.app.base.*;
import gs.wicket.app.account.*;
import gs.wicket.app.admin.*;
import gs.wicket.app.session.*;
import gs.wicket.app.shop.*;
import gs.wicket.app.register.*;

public class LogInPage extends BasePage {

	private String username;
	private String password;
	
	public LogInPage() {
		
		Form form = new Form("form", new CompoundPropertyModel(this)) {
			
			private static final long serialVersionUID = 1L;

			protected void onSubmit() {
				
				try {
					Korisnik korisnik = Korisnici.getKnownKorisnici()
							.getKorisnik(username, password);
					((GSSession) getSession())
							.setLogovanKorisnik(korisnik);
					if(korisnik.getUsername()=="admin" && korisnik.getPassword()=="admin")
					{
						setResponsePage(AdminPage.class);
					}
					else
					{
					setResponsePage(ShopingPage.class);
					}
				} catch (AuthenticationException e) {

					setResponsePage(LogInPage.class);
				}
			
			}
		};
		add(form);
		form.add(new TextField("username"));
		form.add(new PasswordTextField("password"));		
		form.add(new Label("login.page.username", getLangProperty("login.page.username")));
		form.add(new Label("login.page.password", getLangProperty("login.page.password")));

		add(new Label("login.page.title", getLangProperty("login.page.title")));
		
		add(new SubmitLink("loginButton", form) {
			private static final long serialVersionUID = 511246638080207216L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("base.page.search.login"))));
			}
			@Override
			public void onSubmit() {
				
				try {
					Korisnik korisnik = Korisnici.getKnownKorisnici()
							.getKorisnik(username, password);
					((GSSession) getSession())
							.setLogovanKorisnik(korisnik);
					if(korisnik.getUsername()=="admin" && korisnik.getPassword()=="admin")
					{
						setResponsePage(AdminPage.class);
					}
					else
					{
					setResponsePage(ShopingPage.class);
					}
				} catch (AuthenticationException e) {

					setResponsePage(LogInPage.class);
				}				
			
			}
		});
		
		
		Form form1 = new Form("form1") {

			private static final long serialVersionUID = 3699428827779847146L;

			@Override
			protected void onSubmit() {
				setResponsePage(RegisterPage.class);
			}

		};
		add(form1);
		
		add(new SubmitLink("registerButton", form1) {
			private static final long serialVersionUID = 511246638080207216L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("base.page.search.register"))));
			}
			@Override
			public void onSubmit() {
				setResponsePage(RegisterPage.class);			
			}
		});
	}
}